package licode.unisop.types.dp;

import org.springframework.util.Assert;

/**
 * 用户状态
 *
 * @author licode
 */
public class UserState {
    public static final UserState NO_ACTIVATE = new UserState(0);
    public static final UserState ACTIVATED = new UserState(1);
    public static final UserState STOPPED = new UserState(2);
    public static final UserState DELETED = new UserState(3);

    /**
     * 用户状态（0：未激活；1：正常；2：停用；3：删除）
     */
    private final Integer state;

    public static UserState build(int state) {
        Assert.isTrue(state == 0 || state == 1 || state == 2 || state == 3,
                "用户状态错误（取值为0、1、2、3）：" + state);
        if (state == NO_ACTIVATE.getState()) {
            return NO_ACTIVATE;
        } else if (state == ACTIVATED.getState()) {
            return ACTIVATED;
        } else if (state == STOPPED.getState()) {
            return STOPPED;
        } {
            return DELETED;
        }
    }

    public static UserState build(String state) {
        Assert.isTrue("未激活".equals(state) || "正常".equals(state) ||
                        "删除".equals(state) || "停用".equals(state),
                "用户状态错误：" + state);
        if ("未激活".equals(state)) {
            return NO_ACTIVATE;
        } else if ("正常".equals(state)) {
            return ACTIVATED;
        } else if ("停用".equals(state)) {
            return STOPPED;
        } else {
            return DELETED;
        }
    }

    private UserState(int state) {
        Assert.isTrue(state == 0 || state == 1 || state == 2 || state == 3,
                "用户状态错误（取值为0、1、2、3）：" + state);
        this.state = state;
    }

    public Integer getState() {
        return state;
    }

    public boolean isActivated() {
        return this.state == ACTIVATED.getState();
    }

    public boolean isStopped() {
        return this.state == STOPPED.getState();
    }

    public String getStateText() {
        if (0 == this.state) {
            return "未激活";
        } else if (1 == this.state) {
            return "正常";
        } else if (2 == this.state) {
            return "停用";
        }
        return "删除";
    }

    public boolean equals(UserState Other) {
        return this.state == Other.getState();
    }
}
